package org.scau.studentevaluation.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.scau.studentevaluation.client.AttendedStudentListClient;
import org.scau.studentevaluation.config.global.log.WebLog;
import org.scau.studentevaluation.entity.dos.ActDO;
import org.scau.studentevaluation.entity.dos.AttendedStudentListDO;
import org.scau.studentevaluation.entity.query.ActOperateQuery;
import org.scau.studentevaluation.entity.query.ActQuery;
import org.scau.studentevaluation.entity.query.ActUploadQuery;
import org.scau.studentevaluation.entity.vo.ActVO;
import org.scau.studentevaluation.service.IActService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@Api("Act控制器")
@WebLog("Act控制器")
public class ActController {

    @Autowired
    private IActService actService;

    @ApiOperation("获取Act")
    @GetMapping("/findAct/{actId}")
    public ActDO findAct (Long actId){
        return actService.getActByActId(actId);
    }


    @ApiOperation("获取证明")
    @GetMapping("/getCertificationByIsSubmitedAndOrganization")
    public List<ActVO> getCertificationByIsSubmitedAndOrganization(boolean isSubmited, String organization){
        return  actService.getCertificationByIsSubmitedAndOrganization(isSubmited,organization);
    }

    @ApiOperation("上传证明")
    @PostMapping("/uploadCertification")
    public int uploadCertification(@RequestBody ActUploadQuery actQuery){
        return actService.uploadCertification(actQuery);
    }

    @ApiOperation("修改证明(例子:{\"actId\":[\"24\" ,\"25\"] , \"operation\": \"delete\"}")
    @PutMapping("/operateCertificationByActId")
    public int operateCertificationByActId(@RequestBody ActOperateQuery query){
        // 直接用List接受，数组长度为1时会报错
        // 所以先用String接受，解析为JSONArray，再转为List
        JSONArray array = JSON.parseArray(query.getActIds());
        List<Long> actIds = array.stream()
                .map(x -> ((Integer)x).longValue())
                .collect(Collectors.toList());
        return actService.operateCertificationByActId(actIds, query.getOperation());
    }

}
